/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.operation.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.annotation.Inner;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.operation.dto.PledgeDemandDto;
import com.nai365.nart.operation.entity.PledgeDemand;
import com.nai365.nart.operation.service.PledgeDemandService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.nai365.nart.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 质押需求
 *
 * @author baogs
 * @date 2022-07-01 11:13:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/pledge/demand")
@Api(value = "pledge demand", tags = "质押需求管理")
public class PledgeDemandController {

	private final PledgeDemandService pledgeDemandService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param pledgeDemand 质押需求
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R<Page<PledgeDemandDto>> getPledgeDemandPage(Page<PledgeDemand> page, PledgeDemand pledgeDemand) {
		return R.ok(pledgeDemandService.page(page, Wrappers.query(pledgeDemand)));
	}

	@ApiOperation(value = "已发布质押需求", notes = "已发布质押需求")
	@GetMapping("/live")
	@Inner(value = false)
	public R<Page<PledgeDemandDto>> live(Page<PledgeDemand> page) {
		PledgeDemand pledgeDemand = new PledgeDemand();
		pledgeDemand.setIsPublished(true);
		return R.ok(pledgeDemandService.page(page, Wrappers.query(pledgeDemand)));
	}

	/**
	 * 通过id查询质押需求
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@Inner(value = false)
	public R<PledgeDemandDto> getById(@PathVariable("id") Long id) {
		return R.ok(pledgeDemandService.getPledgeDemandDtoById(id));
	}

	/**
	 * 新增质押需求
	 *
	 * @param pledgeDemand 质押需求
	 * @return R
	 */
	@ApiOperation(value = "新增质押需求", notes = "新增质押需求")
	@SysLog("新增质押需求")
	@PostMapping
	public R<PledgeDemand> save(@RequestBody PledgeDemand pledgeDemand) {
		pledgeDemand.setId(null);
		pledgeDemand.setCreateBy(SecurityUtils.getUser().getId());
		pledgeDemand.setUpdateBy(SecurityUtils.getUser().getId());
		pledgeDemand.setCreateTime(LocalDateTime.now());
		pledgeDemand.setUpdateTime(LocalDateTime.now());
		pledgeDemandService.save(pledgeDemand);
		return R.ok(pledgeDemand);
	}

	/**
	 * 修改质押需求
	 *
	 * @param pledgeDemand 质押需求
	 * @return R
	 */
	@ApiOperation(value = "修改质押需求", notes = "修改质押需求")
	@SysLog("修改质押需求")
	@PutMapping
	public R<PledgeDemand> updateById(@RequestBody PledgeDemand pledgeDemand) {
		pledgeDemand.setUpdateBy(SecurityUtils.getUser().getId());
		pledgeDemand.setUpdateTime(LocalDateTime.now());
		pledgeDemandService.updateById(pledgeDemand);
		return R.ok(pledgeDemand);
	}

	/**
	 * 通过id删除质押需求
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除质押需求", notes = "通过id删除质押需求")
	@SysLog("通过id删除质押需求")
	@DeleteMapping("/{id}")
	public R<Boolean> removeById(@PathVariable Long id) {
		return R.ok(pledgeDemandService.removeById(id));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param pledgeDemand 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<PledgeDemand> export(PledgeDemand pledgeDemand) {
		return pledgeDemandService.list(Wrappers.query(pledgeDemand));
	}


}
